package service;

import beans.*;
import dao.*;
import model.ApiResultModel;

import java.util.List;

public class GroupService {

    public static ApiResultModel getGroupList(long user_id) {
        User user = UserDAO.ME.getById(user_id);
        if (user == null) {
            return ApiResultModel.failedWithMessage("用户不存在！");
        }
        List<Group> groupList = GroupDAO.ME.getUserGroupList(user_id);
        return ApiResultModel.successWithObject(groupList);
    }

    public static ApiResultModel addGroup(long admin_id, String group_name) {
        User user = UserDAO.ME.getById(admin_id);
        if (user == null) {
            return ApiResultModel.failedWithMessage("用户不存在！");
        }
        if (!Group.isGroupNameLegal(group_name)) {
            return ApiResultModel.failedWithMessage("群组名长度不合法！");
        }
        Group group = new Group();
        group.setAdmin_id(admin_id);
        group.setGroup_name(group_name);
        long groupId = group.SaveToDB();
        if (groupId <= 0) {
            return ApiResultModel.failedWithMessage("请求失败！");
        }
        UserGroupRelationDAO.ME.addRelation(admin_id, groupId);
        return ApiResultModel.successWithMessage("新建群组成功！");
    }

    public static ApiResultModel getWhiteboardList(long group_id) {
        Group group = GroupDAO.ME.getById(group_id);
        if (group == null) {
            return ApiResultModel.failedWithMessage("群组不存在！");
        }
        List<Whiteboard> whiteboardList = WhiteBoardDAO.ME.getWhiteBoardList(group_id);
        return ApiResultModel.successWithObject(whiteboardList);
    }

    public static ApiResultModel addWhiteboard(long group_id, String board_name) {
        Group group = GroupDAO.ME.getById(group_id);
        if (group == null) {
            return ApiResultModel.failedWithMessage("群组不存在！");
        }
        if (!Whiteboard.isBoardNameLegal(board_name)) {
            return ApiResultModel.failedWithMessage("白板名长度不合法！");
        }
        Whiteboard whiteboard = new Whiteboard();
        whiteboard.setGroup_id(group_id);
        whiteboard.setBoard_name(board_name);
        long id = whiteboard.SaveToDB();
        BoardFile boardFile = new BoardFile();
        boardFile.setBoard_id(id);
        boardFile.SaveToDB();
        return ApiResultModel.successWithMessage("新建白板成功！");
    }

    public static ApiResultModel getMemberList(long group_id) {
        Group group = GroupDAO.ME.getById(group_id);
        if (group == null) {
            return ApiResultModel.failedWithMessage("群组不存在！");
        }
        List<User> userList = UserGroupRelationDAO.ME.getGroupMemberList(group_id);
        for (User user : userList) {
            user.setPwd("");
        }
        return ApiResultModel.successWithObject(userList);
    }

    public static ApiResultModel addMember(long group_id, long user_id) {
        Group group = GroupDAO.ME.getById(group_id);
        if (group == null) {
            return ApiResultModel.failedWithMessage("群组不存在！");
        }
        User user = UserDAO.ME.getById(user_id);
        if (user == null) {
            return ApiResultModel.failedWithMessage("要添加的用户不存在！");
        }
        if (UserGroupRelationDAO.ME.getRelation(user_id, group_id) != null) {
            return ApiResultModel.failedWithMessage("用户已存在群组中！");
        }
        long id = UserGroupRelationDAO.ME.addRelation(user_id, group_id);
        if (id <= 0) {
            return ApiResultModel.failedWithMessage("请求失败！");
        }
        return ApiResultModel.successWithMessage("添加成功！");
    }

    public static ApiResultModel dismissGroup(long group_id) {
        Group group = GroupDAO.ME.getById(group_id);
        if (group == null) {
            return ApiResultModel.failedWithMessage("群组不存在！");
        }
        group.Delete();
        WhiteBoardDAO.ME.deleteGroupWhiteBoards(group_id);
        UserGroupRelationDAO.ME.deleteGroupRelation(group_id);
        return ApiResultModel.successWithMessage("群组已解散！");
    }

    public static ApiResultModel removeMember(long user_id, long group_id) {
        UserGroupRelation relation = UserGroupRelationDAO.ME.getRelation(user_id, group_id);
        relation.Delete();
        return ApiResultModel.successWithMessage("成员移除成功！");
    }

    public static ApiResultModel removeBoard(long group_id, long board_id) {
        Whiteboard whiteboard = WhiteBoardDAO.ME.getById(board_id);
        whiteboard.Delete();
        BoardFileDAO.ME.deleteBoardFile(board_id);
        return ApiResultModel.successWithMessage("白板<"+whiteboard.getBoard_name()+">已被移除！");
    }

}
